/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bc;

import edu.upn.sigemfi.ope.bc.CanalFacadeLocal;
import edu.upn.sigemfi.ope.be.Canal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Gabo (U)
 */
public class CanalDelegate {

    private CanalFacadeLocal lookupCanalFacade() {
        try {
            Context c = new InitialContext();
            return (CanalFacadeLocal) c.lookup("java:comp/env/CanalFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private CanalFacadeLocal local;

    public CanalDelegate(CanalFacadeLocal local) {
        this.local = local;
    }
        public void crear(Canal canal) throws Exception {
        try {
            local.create(canal);
        } catch (Exception ex) {
            local = lookupCanalFacade();
            throw new Exception("Crear Canal... " + ex.getMessage());
        }
    }
        public void editar(Canal canal) throws Exception {
        try {
            local.edit(canal);
        } catch (Exception ex) {
            local = lookupCanalFacade();
            throw new Exception("Editar Canal... " + ex.getMessage());
        }
    }
        public void eliminar(Canal canal) throws Exception {
        try {
            local.remove(canal);
        } catch (Exception ex) {
            local = lookupCanalFacade();
            throw new Exception("Eliminar Canal... " + ex.getMessage());
        }
    }
        public List<Canal> getCanales() throws Exception {
        List<Canal> canales;
        try {
            canales = local.findAll();
        } catch (Exception ex) {
            local = lookupCanalFacade();
            throw new Exception("Listar Canales... " + ex.getMessage());
        }
        return canales;
    }

}
